/*-------------------------------------------------------------------------
 *
 *	  EUC_TW, BIG5 和 MULE_INTERNAL
 *
 * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 * IDENTIFICATION
 *	  src/backend/utils/mb/conversion_procs/euc_tw_and_big5/euc_tw_and_big5.c
 *
 *-------------------------------------------------------------------------
 */

#include "postgres.h"
#include "fmgr.h"
#include "mb/pg_wchar.h"

#define ENCODING_GROWTH_RATE 4

PG_MODULE_MAGIC;

PG_FUNCTION_INFO_V1(euc_tw_to_big5);
PG_FUNCTION_INFO_V1(big5_to_euc_tw);
PG_FUNCTION_INFO_V1(euc_tw_to_mic);
PG_FUNCTION_INFO_V1(mic_to_euc_tw);
PG_FUNCTION_INFO_V1(big5_to_mic);
PG_FUNCTION_INFO_V1(mic_to_big5);

/* ----------
 * conv_proc(
 *		INTEGER,	-- 源编码id
 *		INTEGER,	-- 目标编码id
 *		CSTRING,	-- 源字符串（以null结尾的C字符串）
 *		CSTRING,	-- 目标字符串（以null结尾的C字符串）
 *		INTEGER,	-- 源字符串长度
 *		BOOL		-- 如果为真，转换失败时不抛出错误
 * ) 返回INTEGER;
 *
 * 返回成功转换的字节数。
 * ----------
 */

static int	fc_euc_tw2big5(const unsigned char *fc_euc, unsigned char *fc_p, int fc_len, bool fc_noError);
static int	fc_big52euc_tw(const unsigned char *fc_euc, unsigned char *fc_p, int fc_len, bool fc_noError);
static int	fc_big52mic(const unsigned char *fc_big5, unsigned char *fc_p, int fc_len, bool fc_noError);
static int	fc_mic2big5(const unsigned char *fc_mic, unsigned char *fc_p, int fc_len, bool fc_noError);
static int	fc_euc_tw2mic(const unsigned char *fc_euc, unsigned char *fc_p, int fc_len, bool fc_noError);
static int	fc_mic2euc_tw(const unsigned char *fc_mic, unsigned char *fc_p, int fc_len, bool fc_noError);

Datum euc_tw_to_big5(PG_FUNCTION_ARGS)
{
	unsigned char *fc_src = (unsigned char *) PG_GETARG_CSTRING(2);
	unsigned char *fc_dest = (unsigned char *) PG_GETARG_CSTRING(3);
	int			fc_len = PG_GETARG_INT32(4);
	bool		fc_noError = PG_GETARG_BOOL(5);
	int			fc_converted;

	CHECK_ENCODING_CONVERSION_ARGS(PG_EUC_TW, PG_BIG5);

	fc_converted = fc_euc_tw2big5(fc_src, fc_dest, fc_len, fc_noError);

	PG_RETURN_INT32(fc_converted);
}

Datum big5_to_euc_tw(PG_FUNCTION_ARGS)
{
	unsigned char *fc_src = (unsigned char *) PG_GETARG_CSTRING(2);
	unsigned char *fc_dest = (unsigned char *) PG_GETARG_CSTRING(3);
	int			fc_len = PG_GETARG_INT32(4);
	bool		fc_noError = PG_GETARG_BOOL(5);
	int			fc_converted;

	CHECK_ENCODING_CONVERSION_ARGS(PG_BIG5, PG_EUC_TW);

	fc_converted = fc_big52euc_tw(fc_src, fc_dest, fc_len, fc_noError);

	PG_RETURN_INT32(fc_converted);
}

Datum euc_tw_to_mic(PG_FUNCTION_ARGS)
{
	unsigned char *fc_src = (unsigned char *) PG_GETARG_CSTRING(2);
	unsigned char *fc_dest = (unsigned char *) PG_GETARG_CSTRING(3);
	int			fc_len = PG_GETARG_INT32(4);
	bool		fc_noError = PG_GETARG_BOOL(5);
	int			fc_converted;

	CHECK_ENCODING_CONVERSION_ARGS(PG_EUC_TW, PG_MULE_INTERNAL);

	fc_converted = fc_euc_tw2mic(fc_src, fc_dest, fc_len, fc_noError);

	PG_RETURN_INT32(fc_converted);
}

Datum mic_to_euc_tw(PG_FUNCTION_ARGS)
{
	unsigned char *fc_src = (unsigned char *) PG_GETARG_CSTRING(2);
	unsigned char *fc_dest = (unsigned char *) PG_GETARG_CSTRING(3);
	int			fc_len = PG_GETARG_INT32(4);
	bool		fc_noError = PG_GETARG_BOOL(5);
	int			fc_converted;

	CHECK_ENCODING_CONVERSION_ARGS(PG_MULE_INTERNAL, PG_EUC_TW);

	fc_converted = fc_mic2euc_tw(fc_src, fc_dest, fc_len, fc_noError);

	PG_RETURN_INT32(fc_converted);
}

Datum big5_to_mic(PG_FUNCTION_ARGS)
{
	unsigned char *fc_src = (unsigned char *) PG_GETARG_CSTRING(2);
	unsigned char *fc_dest = (unsigned char *) PG_GETARG_CSTRING(3);
	int			fc_len = PG_GETARG_INT32(4);
	bool		fc_noError = PG_GETARG_BOOL(5);
	int			fc_converted;

	CHECK_ENCODING_CONVERSION_ARGS(PG_BIG5, PG_MULE_INTERNAL);

	fc_converted = fc_big52mic(fc_src, fc_dest, fc_len, fc_noError);

	PG_RETURN_INT32(fc_converted);
}

Datum mic_to_big5(PG_FUNCTION_ARGS)
{
	unsigned char *fc_src = (unsigned char *) PG_GETARG_CSTRING(2);
	unsigned char *fc_dest = (unsigned char *) PG_GETARG_CSTRING(3);
	int			fc_len = PG_GETARG_INT32(4);
	bool		fc_noError = PG_GETARG_BOOL(5);
	int			fc_converted;

	CHECK_ENCODING_CONVERSION_ARGS(PG_MULE_INTERNAL, PG_BIG5);

	fc_converted = fc_mic2big5(fc_src, fc_dest, fc_len, fc_noError);

	PG_RETURN_INT32(fc_converted);
}


/*
 * EUC_TW ---> Big5
 */
static int fc_euc_tw2big5(const unsigned char *fc_euc, unsigned char *fc_p, int fc_len, bool fc_noError)
{
	const unsigned char *fc_start = fc_euc;
	unsigned char fc_c1;
	unsigned short fc_big5buf,
				fc_cnsBuf;
	unsigned char fc_lc;
	int			fc_l;

	while (fc_len > 0)
	{
		fc_c1 = *fc_euc;
		if (IS_HIGHBIT_SET(fc_c1))
		{
			/* 验证并解码下一个 EUC_TW 输入字符 */
			fc_l = pg_encoding_verifymbchar(PG_EUC_TW, (const char *) fc_euc, fc_len);
			if (fc_l < 0)
			{
				if (fc_noError)
					break;
				report_invalid_encoding(PG_EUC_TW,
										(const char *) fc_euc, fc_len);
			}
			if (fc_c1 == SS2)
			{
				fc_c1 = fc_euc[1];	/* 平面编号 */
				if (fc_c1 == 0xa1)
					fc_lc = LC_CNS11643_1;
				else if (fc_c1 == 0xa2)
					fc_lc = LC_CNS11643_2;
				else
					fc_lc = fc_c1 - 0xa3 + LC_CNS11643_3;
				fc_cnsBuf = (fc_euc[2] << 8) | fc_euc[3];
			}
			else
			{					/* CNS11643-1 */
				fc_lc = LC_CNS11643_1;
				fc_cnsBuf = (fc_c1 << 8) | fc_euc[1];
			}

			/* 以 Big5 格式输出 */
			fc_big5buf = CNStoBIG5(fc_cnsBuf, fc_lc);
			if (fc_big5buf == 0)
			{
				if (fc_noError)
					break;
				report_untranslatable_char(PG_EUC_TW, PG_BIG5,
										   (const char *) fc_euc, fc_len);
			}
			*fc_p++ = (fc_big5buf >> 8) & 0x00ff;
			*fc_p++ = fc_big5buf & 0x00ff;

			fc_euc += fc_l;
			fc_len -= fc_l;
		}
		else
		{						/* 应该是 ASCII */
			if (fc_c1 == 0)
			{
				if (fc_noError)
					break;
				report_invalid_encoding(PG_EUC_TW,
										(const char *) fc_euc, fc_len);
			}
			*fc_p++ = fc_c1;
			fc_euc++;
			fc_len--;
		}
	}
	*fc_p = '\0';

	return fc_euc - fc_start;
}

/*
 * Big5 ---> EUC_TW
 */
static int fc_big52euc_tw(const unsigned char *fc_big5, unsigned char *fc_p, int fc_len, bool fc_noError)
{
	const unsigned char *fc_start = fc_big5;
	unsigned short fc_c1;
	unsigned short fc_big5buf,
				fc_cnsBuf;
	unsigned char fc_lc;
	int			fc_l;

	while (fc_len > 0)
	{
		/* 验证并解码下一个 Big5 输入字符 */
		fc_c1 = *fc_big5;
		if (IS_HIGHBIT_SET(fc_c1))
		{
			fc_l = pg_encoding_verifymbchar(PG_BIG5, (const char *) fc_big5, fc_len);
			if (fc_l < 0)
			{
				if (fc_noError)
					break;
				report_invalid_encoding(PG_BIG5,
										(const char *) fc_big5, fc_len);
			}
			fc_big5buf = (fc_c1 << 8) | fc_big5[1];
			fc_cnsBuf = BIG5toCNS(fc_big5buf, &fc_lc);

			if (fc_lc == LC_CNS11643_1)
			{
				*fc_p++ = (fc_cnsBuf >> 8) & 0x00ff;
				*fc_p++ = fc_cnsBuf & 0x00ff;
			}
			else if (fc_lc == LC_CNS11643_2)
			{
				*fc_p++ = SS2;
				*fc_p++ = 0xa2;
				*fc_p++ = (fc_cnsBuf >> 8) & 0x00ff;
				*fc_p++ = fc_cnsBuf & 0x00ff;
			}
			else if (fc_lc >= LC_CNS11643_3 && fc_lc <= LC_CNS11643_7)
			{
				*fc_p++ = SS2;
				*fc_p++ = fc_lc - LC_CNS11643_3 + 0xa3;
				*fc_p++ = (fc_cnsBuf >> 8) & 0x00ff;
				*fc_p++ = fc_cnsBuf & 0x00ff;
			}
			else
			{
				if (fc_noError)
					break;
				report_untranslatable_char(PG_BIG5, PG_EUC_TW,
										   (const char *) fc_big5, fc_len);
			}

			fc_big5 += fc_l;
			fc_len -= fc_l;
		}
		else
		{
			/* ASCII */
			if (fc_c1 == 0)
			{
				if (fc_noError)
					break;
				report_invalid_encoding(PG_BIG5,
										(const char *) fc_big5, fc_len);
			}
			*fc_p++ = fc_c1;
			fc_big5++;
			fc_len--;
			continue;
		}
	}
	*fc_p = '\0';

	return fc_big5 - fc_start;
}

/*
 * EUC_TW ---> MIC
 */
static int fc_euc_tw2mic(const unsigned char *fc_euc, unsigned char *fc_p, int fc_len, bool fc_noError)
{
	const unsigned char *fc_start = fc_euc;
	int			fc_c1;
	int			fc_l;

	while (fc_len > 0)
	{
		fc_c1 = *fc_euc;
		if (IS_HIGHBIT_SET(fc_c1))
		{
			fc_l = pg_encoding_verifymbchar(PG_EUC_TW, (const char *) fc_euc, fc_len);
			if (fc_l < 0)
			{
				if (fc_noError)
					break;
				report_invalid_encoding(PG_EUC_TW,
										(const char *) fc_euc, fc_len);
			}
			if (fc_c1 == SS2)
			{
				fc_c1 = fc_euc[1];	/* 平面编号 */
				if (fc_c1 == 0xa1)
					*fc_p++ = LC_CNS11643_1;
				else if (fc_c1 == 0xa2)
					*fc_p++ = LC_CNS11643_2;
				else
				{
					/* 其他平面是 MULE 私有字符集 */
					*fc_p++ = LCPRV2_B;
					*fc_p++ = fc_c1 - 0xa3 + LC_CNS11643_3;
				}
				*fc_p++ = fc_euc[2];
				*fc_p++ = fc_euc[3];
			}
			else
			{					/* CNS11643-1 */
				*fc_p++ = LC_CNS11643_1;
				*fc_p++ = fc_c1;
				*fc_p++ = fc_euc[1];
			}
			fc_euc += fc_l;
			fc_len -= fc_l;
		}
		else
		{						/* 应该是 ASCII */
			if (fc_c1 == 0)
			{
				if (fc_noError)
					break;
				report_invalid_encoding(PG_EUC_TW,
										(const char *) fc_euc, fc_len);
			}
			*fc_p++ = fc_c1;
			fc_euc++;
			fc_len--;
		}
	}
	*fc_p = '\0';

	return fc_euc - fc_start;
}

/*
 * MIC ---> EUC_TW
 */
static int fc_mic2euc_tw(const unsigned char *fc_mic, unsigned char *fc_p, int fc_len, bool fc_noError)
{
	const unsigned char *fc_start = fc_mic;
	int			fc_c1;
	int			fc_l;

	while (fc_len > 0)
	{
		fc_c1 = *fc_mic;
		if (!IS_HIGHBIT_SET(fc_c1))
		{
			/* ASCII */
			if (fc_c1 == 0)
			{
				if (fc_noError)
					break;
				report_invalid_encoding(PG_MULE_INTERNAL,
										(const char *) fc_mic, fc_len);
			}
			*fc_p++ = fc_c1;
			fc_mic++;
			fc_len--;
			continue;
		}
		fc_l = pg_encoding_verifymbchar(PG_MULE_INTERNAL, (const char *) fc_mic, fc_len);
		if (fc_l < 0)
		{
			if (fc_noError)
				break;
			report_invalid_encoding(PG_MULE_INTERNAL,
									(const char *) fc_mic, fc_len);
		}
		if (fc_c1 == LC_CNS11643_1)
		{
			*fc_p++ = fc_mic[1];
			*fc_p++ = fc_mic[2];
		}
		else if (fc_c1 == LC_CNS11643_2)
		{
			*fc_p++ = SS2;
			*fc_p++ = 0xa2;
			*fc_p++ = fc_mic[1];
			*fc_p++ = fc_mic[2];
		}
		else if (fc_c1 == LCPRV2_B &&
				 fc_mic[1] >= LC_CNS11643_3 && fc_mic[1] <= LC_CNS11643_7)
		{
			*fc_p++ = SS2;
			*fc_p++ = fc_mic[1] - LC_CNS11643_3 + 0xa3;
			*fc_p++ = fc_mic[2];
			*fc_p++ = fc_mic[3];
		}
		else
		{
			if (fc_noError)
				break;
			report_untranslatable_char(PG_MULE_INTERNAL, PG_EUC_TW,
									   (const char *) fc_mic, fc_len);
		}
		fc_mic += fc_l;
		fc_len -= fc_l;
	}
	*fc_p = '\0';

	return fc_mic - fc_start;
}

/*
 * Big5 ---> MIC
 */
static int fc_big52mic(const unsigned char *fc_big5, unsigned char *fc_p, int fc_len, bool fc_noError)
{
	const unsigned char *fc_start = fc_big5;
	unsigned short fc_c1;
	unsigned short fc_big5buf,
				fc_cnsBuf;
	unsigned char fc_lc;
	int			fc_l;

	while (fc_len > 0)
	{
		fc_c1 = *fc_big5;
		if (!IS_HIGHBIT_SET(fc_c1))
		{
			/* ASCII */
			if (fc_c1 == 0)
			{
				if (fc_noError)
					break;
				report_invalid_encoding(PG_BIG5,
										(const char *) fc_big5, fc_len);
			}
			*fc_p++ = fc_c1;
			fc_big5++;
			fc_len--;
			continue;
		}
		fc_l = pg_encoding_verifymbchar(PG_BIG5, (const char *) fc_big5, fc_len);
		if (fc_l < 0)
		{
			if (fc_noError)
				break;
			report_invalid_encoding(PG_BIG5,
									(const char *) fc_big5, fc_len);
		}
		fc_big5buf = (fc_c1 << 8) | fc_big5[1];
		fc_cnsBuf = BIG5toCNS(fc_big5buf, &fc_lc);
		if (fc_lc != 0)
		{
			/* 平面 3 和 4 是 MULE 私有字符集 */
			if (fc_lc == LC_CNS11643_3 || fc_lc == LC_CNS11643_4)
				*fc_p++ = LCPRV2_B;
			*fc_p++ = fc_lc;			/* 飞机编号 */
			*fc_p++ = (fc_cnsBuf >> 8) & 0x00ff;
			*fc_p++ = fc_cnsBuf & 0x00ff;
		}
		else
		{
			if (fc_noError)
				break;
			report_untranslatable_char(PG_BIG5, PG_MULE_INTERNAL,
									   (const char *) fc_big5, fc_len);
		}
		fc_big5 += fc_l;
		fc_len -= fc_l;
	}
	*fc_p = '\0';

	return fc_big5 - fc_start;
}

/*
 * MIC ---> Big5
 */
static int fc_mic2big5(const unsigned char *fc_mic, unsigned char *fc_p, int fc_len, bool fc_noError)
{
	const unsigned char *fc_start = fc_mic;
	unsigned short fc_c1;
	unsigned short fc_big5buf,
				fc_cnsBuf;
	int			fc_l;

	while (fc_len > 0)
	{
		fc_c1 = *fc_mic;
		if (!IS_HIGHBIT_SET(fc_c1))
		{
			/* ASCII */
			if (fc_c1 == 0)
			{
				if (fc_noError)
					break;
				report_invalid_encoding(PG_MULE_INTERNAL,
										(const char *) fc_mic, fc_len);
			}
			*fc_p++ = fc_c1;
			fc_mic++;
			fc_len--;
			continue;
		}
		fc_l = pg_encoding_verifymbchar(PG_MULE_INTERNAL, (const char *) fc_mic, fc_len);
		if (fc_l < 0)
		{
			if (fc_noError)
				break;
			report_invalid_encoding(PG_MULE_INTERNAL,
									(const char *) fc_mic, fc_len);
		}
		if (fc_c1 == LC_CNS11643_1 || fc_c1 == LC_CNS11643_2 || fc_c1 == LCPRV2_B)
		{
			if (fc_c1 == LCPRV2_B)
			{
				fc_c1 = fc_mic[1];	/* 获取飞机编号 */
				fc_cnsBuf = (fc_mic[2] << 8) | fc_mic[3];
			}
			else
			{
				fc_cnsBuf = (fc_mic[1] << 8) | fc_mic[2];
			}
			fc_big5buf = CNStoBIG5(fc_cnsBuf, fc_c1);
			if (fc_big5buf == 0)
			{
				if (fc_noError)
					break;
				report_untranslatable_char(PG_MULE_INTERNAL, PG_BIG5,
										   (const char *) fc_mic, fc_len);
			}
			*fc_p++ = (fc_big5buf >> 8) & 0x00ff;
			*fc_p++ = fc_big5buf & 0x00ff;
		}
		else
		{
			if (fc_noError)
				break;
			report_untranslatable_char(PG_MULE_INTERNAL, PG_BIG5,
									   (const char *) fc_mic, fc_len);
		}
		fc_mic += fc_l;
		fc_len -= fc_l;
	}
	*fc_p = '\0';

	return fc_mic - fc_start;
}
